package com.senarai.thedemit;

import java.util.Calendar;
import java.util.Random;

public class Randomizer {
	
	//	pasaran
	private static final String _pon = "10000";		//	1
	private static final String _wage = "11111";	//	5
	private static final String _kliwon = "11111";	//	4
	private static final String _legi = "11000";		//	2
	private static final String _pahing = "11100";		//	3
	private static final String[]  _pasaran = new String[] {_pon, _wage, _kliwon, _legi, _pahing};
	
	//	days
	private static final String sunday = "1100000";		//	2
	private static final String monday = "1111100";		//	5
	private static final String tuesday = "1110000";	// 3
	private static final String wednesday = "1100000";	// 2
	private static final String thursday = "1111111";	// 7
	private static final String friday = "1111100";		// 	5
	private static final String saturday = "1111100";	//	5
	private static final String[] days = new String[] {sunday, monday, tuesday, wednesday, friday, thursday, saturday};
	
	//	time
	private static final String morning = "11000";		// 5-9
	private static final String noon = "10000";			//	9-15
	private static final String afternoon = "11100";	//	15-20
	private static final String night = "11110";		//	20-24
	private static final String latenight = "11111";	//	24-5
	private static final int morning_id = 0;
	private static final int noon_id = 1;
	private static final int afternoon_id = 2;
	private static final int nigth_id = 3;
	private static final int latenight_id = 4;
	private static final String[] times = new String[] {morning, noon, afternoon, night, latenight};
	
	public static Random myRandom = new Random();
	
	/**
	 * isGhostAppear function
	 * if ghost will appear return true, otherwise false
	 * @return
	 */
	public static boolean isGhostAppear(){
		if (getDayProb() == 0) return true;
		else {
			if (getTimeProb() == 0) return true;
			else{
				if (getPasaranProb() == 0) return true;
				else return true;
			}
		}
	}
	
	/**
	 * random function 
	 * @return integer
	 */
	public static int randomizer(int n){
		return myRandom.nextInt(n);
	}
	
	public static float randomizer(float f){
		return (myRandom.nextFloat() * f);
	}
	
	/**
	 * random day effect
	 * @return
	 */
	public static int getDayProb(){
		Calendar calendar = Calendar.getInstance();
		int day = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		String probString = days[day];
		char probChar = probString.charAt(randomizer(6));
		if (probChar == '1') return 1;
		else	return 0;
	}
	
	/**
	 * random time effect
	 * @return
	 */
	public static int getTimeProb(){
		Calendar calendar = Calendar.getInstance();
		int time = calendar.get(Calendar.HOUR_OF_DAY);
		String probString;
		if (time >= 5 && time < 9) probString = times[morning_id];
		else if (time >= 9  && time < 15) probString = times[noon_id];
		else if (time >= 15 && time < 20) probString = times[afternoon_id];
		else if (time >= 20 && time < 24) probString = times[nigth_id];
		else probString = times[latenight_id];
		
		char probChar = probString.charAt(randomizer(5));
		if (probChar == '1') return 1;
		else	return 0;
	}

	/**
	 * random pasaran effect
	 * @return
	 */
	public static int getPasaranProb(){
		String probString = _pasaran[getTodayPasaran()];
		char probChar = probString.charAt(randomizer(5));
		if (probChar == '1') return 1;
		else	return 0;
	}
	
	/**
	 * random date effect
	 * i.e. 4, 13 is the most, 6, 14, 24, is next
	 * @return
	 */
	public 	static int getDateProb(){
		//	not implemented yet
		return 1;
	}
	
	/**
	 * random user gender effect
	 * @return
	 */
	public static int getGenderProb(){
		//	not implemented yet
		return 1;		
	}
	
	//	get for today's pasaran 
	public static int getTodayPasaran(){
		Calendar firstPon = Calendar.getInstance();
		firstPon.set(Calendar.DAY_OF_MONTH,13);
		firstPon.set(Calendar.MONTH,6); // 0-11 so 1 less
		firstPon.set(Calendar.YEAR, 2011);
		//	13 July is my birthday
		
		Calendar today = Calendar.getInstance();
		long diff = today.getTimeInMillis() - firstPon.getTimeInMillis();
		long day = diff / (24 * 60 * 60 * 1000);
		return ((int) ((day + 1) % 5));
	}
}
